/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package spriteeditor.sprite;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import spriteeditor.SpriteEditorView;


/**
 *
 * @author $L!M
 */
public class Frame extends JPanel
{
    private int x, y, id;
    private BufferedImage img;
    private boolean isMouseOver;
    public final static int SIZE = 128;
    public int getId()
    {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Frame (int id, int x, int y, BufferedImage img)
    {
        super();
        setPreferredSize(new Dimension(SIZE, SIZE));
        setSize(SIZE, SIZE);
        this.x = x;
        this.y = y;
        this.img = img;
        this.id = id;
        this.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                SpriteEditorView.getSprite().currAnimAddFrame((Frame)e.getSource());
            }

            public void mousePressed(MouseEvent e) {
               
            }

            public void mouseReleased(MouseEvent e) {
               
            }

            public void mouseEntered(MouseEvent e) {
                Frame f = (Frame)e.getSource();
                f.isMouseOver = true;
                f.getParent().repaint();
            }

            public void mouseExited(MouseEvent e) {
               Frame f = (Frame)e.getSource();
                f.isMouseOver = false;
                f.getParent().repaint();
            }
        });
    }
    public BufferedImage getImg()
    {
        return img;
    }

    public void paintFrame(Graphics g, int x, int y)
    {
        g.drawImage(getImg(), x, y, null);
    }

    public void setImg(BufferedImage img)
    {
        this.img = img;
    }

    
    public int getXIndex()
    {
        return x;
    }

    public void setXIndex(int x)
    {
        this.x = x;
    }

    public int getYindex()
    {
        return y;
    }

    public void setYIndex(int y) {
        this.y = y;
    }
    @Override
    public void paintComponent(Graphics g)
    {
        int imgX = SIZE / 2 - img.getWidth() / 2;
        int imgY = SIZE / 2 - img.getHeight() / 2;
        g.setColor(Color.GRAY);
        if(isMouseOver)
        {
            g.fillRect(0, 0, getWidth(), getHeight());
        }
        g.drawImage(img, imgX, imgY, null);

        String s = String.valueOf(id);
        g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
        int strW = g.getFontMetrics().stringWidth(s);
        g.drawString(s, SIZE/2 - strW /2, SIZE - 3);
    }
}
